package ybl.qianchuan;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson2.JSONObject;

/**
 * LiveDataManage类用于管理实时数据
 */
public class LiveDataManage {
    private static final QianChuanConfig QIANCHUAN_CONFIG = ConfigSingleton.getInstance();
    private static final String OPEN_API_URL_PREFIX = QIANCHUAN_CONFIG.getQIANCHUAN().getOPEN_API_URL_PREFIX();

    /**
     * 根据直播开始日期获取直播间信息
     * 
     * @param accessToken  用户访问令牌
     * @param advertiserId 广告主ID
     * @param awemeId      视频ID
     * @param dateTime     日期时间
     * @param roomStatus   房间状态
     * @param adStatus     广告状态
     * @param fields       请求返回字段
     * @param page         分页数
     * @param pageSize     分页大小
     * @return 返回JSON对象
     */
    public static JSONObject getRoomsByStartDay(String accessToken, Long advertiserId,
            Long awemeId, String dateTime, String roomStatus,
            String adStatus, List<String> fields, int page, int pageSize) {
        Map<String, Object> params = new HashMap<>() {
            {
                put("advertiser_id", advertiserId);
                put("aweme_id", awemeId);
                put("date_time", dateTime);
                put("room_status", roomStatus);
                put("ad_status", adStatus);
                put("fields", fields);
                put("page", page);
                put("page_size", pageSize);
            }
        };
        return HttpRequest.get(OPEN_API_URL_PREFIX + "v1.0/qianchuan/today_live/room/get/", params, accessToken);
    }

    /**
     * 根据直播开始日期获取直播间信息（默认获取指定字段下的前20条数据）
     * 
     * @param accessToken  用户访问令牌
     * @param advertiserId 广告主ID
     * @param awemeId      视频ID
     * @param dateTime     日期时间
     * @param roomStatus   房间状态
     * @return 返回JSON对象
     */
    public static JSONObject getRoomsByStartDay(String accessToken, Long advertiserId, Long awemeId, String dateTime,
            String roomStatus) {
        List<String> fieldsList = List.of(
                "stat_cost",
                "live_pay_order_gmv_alias",
                "luban_live_pay_order_gmv",
                // "live_pay_order_gmv_roi",
                "ad_live_prepay_and_pay_order_gmv_roi");
        return getRoomsByStartDay(accessToken, advertiserId, awemeId, dateTime, roomStatus, "ALL", fieldsList, 1, 20);
    }

    /**
     * 获取指定日期的直播数据
     * 
     * @param accessToken  用户访问令牌
     * @param advertiserId 广告主ID
     * @param awemeId      视频ID
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param fields       请求返回字段
     * @return 返回JSON对象
     */
    public static JSONObject getOneDayLiveData(String accessToken, Long advertiserId, Long awemeId, String startTime,
            String endTime, List<String> fields) {

        Map<String, Object> params = new HashMap<>() {
            {
                put("advertiser_id", advertiserId);
                put("aweme_id", awemeId);
                put("start_time", startTime);
                put("end_time", endTime);
                put("fields", fields);
            }
        };
        return HttpRequest.get(OPEN_API_URL_PREFIX + "v1.0/qianchuan/report/live/get/", params, accessToken);
    }

    /**
     * 获取指定日期的直播数据（默认获取指定字段下的数据）
     * 
     * @param accessToken  用户访问令牌
     * @param advertiserId 广告主ID
     * @param awemeId      视频ID
     * @return 返回JSON对象
     */
    public static JSONObject getOneDayLiveData(String accessToken, Long advertiserId, Long awemeId, String oneDay) {
        List<String> fieldsList = List.of(
                "stat_cost",
                "live_pay_order_gmv_alias",
                "luban_live_pay_order_gmv",
                // "live_pay_order_gmv_roi",
                "ad_live_prepay_and_pay_order_gmv_roi");
        return getOneDayLiveData(accessToken, advertiserId, awemeId, oneDay + " 00:00:00", oneDay + " 23:59:59",
                fieldsList);
    }

    // 获取全域投放数据
    public static JSONObject uniPromotion(String accessToken, Long advertiserId, Long awemeId, String startTime,
            String endTime, List<String> fields) {
        Map<String, Object> params = new HashMap<>() {
            {
                put("advertiser_id", advertiserId);
                put("aweme_id", awemeId);
                put("metrics", fields);
                put("start_time", startTime);
                put("end_time", endTime);
                put("dimension ", "TIME_GRANULARITY_DAILY");
            }
        };
        return HttpRequest.get(OPEN_API_URL_PREFIX + "v1.0/qianchuan/report/uni_promotion/dimension_data/author/get/",
                params, accessToken);
    }

    public static JSONObject uniPromotion(String accessToken, Long advertiserId, Long awemeId, String startTime,
            String endTime) {
        List<String> fieldsList = List.of(
                "stat_cost",
                "total_prepay_and_pay_order_roi2",
                "total_pay_order_gmv_for_roi2",
                "total_pay_order_count_for_roi2",
                "total_cost_per_pay_order_for_roi2",
                "total_pay_order_coupon_amount_for_roi2",
                "total_prepay_order_count_for_roi2",
                "total_unfinished_estimate_order_gmv_for_roi2",
                "total_prepay_order_gmv_for_roi2");
        return uniPromotion(accessToken, advertiserId, awemeId, startTime, endTime, fieldsList);
    }
}